//
//  ICMProgressHeaderView.m
//  ICrEAM
//
//  Created by rainimute on 16/10/28.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ICMProgressHeaderView.h"

@interface ICMProgressHeaderView ()

@property (nonatomic, strong) NSDictionary *stateDic;

@property (nonatomic, strong) NSArray *progressNormalImages;

@property (nonatomic, strong) NSArray *progressSelectedImages;

@property (nonatomic, strong) NSMutableArray *progressButtons;
@property (nonatomic, strong) NSMutableArray *progressButtonsBigger;

@property (nonatomic, strong) NSMutableArray *progressLineLabels;

@property (nonatomic, strong) NSMutableArray *progressLabels;

@end

@implementation ICMProgressHeaderView

- (instancetype)initProgressHeaderViewWithStateDic:(NSDictionary *)stateDic andFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = BackgroundColor_HeaderGray;
        if(stateDic == nil){
//            self.stateDic = @{@"0":@"0",@"1":@"123"};
        }else{
            self.stateDic = stateDic;
        }
        for (UIButton *progressButton in self.progressButtons) {
            [self addSubview:progressButton];
        }
        for (UIButton *progressButton in self.progressButtonsBigger) {
            [self addSubview:progressButton];
        }
        for (UILabel *progressLabel in self.progressLabels) {
            [self addSubview:progressLabel];
        }
        for (UILabel *progressLineLabel in self.progressLineLabels) {
            [self addSubview:progressLineLabel];
        }
    }
    return self;
}


- (void)progressButtonAction:(UIButton*)sender{
    NSInteger index = [self.progressButtons containsObject:sender]?[self.progressButtons indexOfObject:sender]:[self.progressButtonsBigger indexOfObject:sender];
    if ([self.delegate respondsToSelector:@selector(progressHeaderViewDidSelectedIndex:)]) {
        [self.delegate progressHeaderViewDidSelectedIndex:index];
    }
}

- (NSMutableArray *)progressButtons{
    if (!_progressButtons) {
        _progressButtons = [NSMutableArray array];
        _progressButtonsBigger = [NSMutableArray array];
        for (NSInteger i=0; i<4; i++) {
            [self addProgressButtonWithIndex:i];
        }
    }
    return _progressButtons;
}

- (void)addProgressButtonWithIndex:(NSInteger)index{
    UIButton *progressButton = [[UIButton alloc]initWithFrame:CGRectMake(ActualWidth(156)+(ScreenWidth-(2*ActualWidth(186)))*index/3, ActualHeight(5), ActualWidth(30), ActualHeight(30))];
    [progressButton setUpButtonWithNormolTitle:nil selectedTitle:nil normolTitleSize:0 selectedTitleSize:0 normolTitleColor:nil selectedTitleColor:nil normolBcakImage:self.progressNormalImages[index] selectedBackImage:self.progressSelectedImages[index] actionSEL:@selector(progressButtonAction:) target:self];
    progressButton.selected = [[self.stateDic allKeys] containsObject:[@(index) stringValue]];
    progressButton.userInteractionEnabled = (self.stateDic[[@(index) stringValue]])&&(![self.stateDic[[@(index) stringValue]] isEqualToString:@""]);
    [_progressButtons addObject:progressButton];
    UIButton *progressButtonBigger = [[UIButton alloc]initWithFrame:CGRectMake(progressButton.leftEdge-20, progressButton.topEdge, progressButton.width+40, progressButton.height+25)];
    [progressButtonBigger setUpButtonWithNormolTitle:nil selectedTitle:nil normolTitleSize:0 selectedTitleSize:0 normolTitleColor:nil selectedTitleColor:nil normolBcakImage:nil selectedBackImage:nil actionSEL:@selector(progressButtonAction:) target:self];
    progressButtonBigger.backgroundColor = [UIColor clearColor];
    progressButtonBigger.userInteractionEnabled = (self.stateDic[[@(index) stringValue]])&&(![self.stateDic[[@(index) stringValue]] isEqualToString:@""]);
    [_progressButtonsBigger addObject:progressButtonBigger];
}

- (NSArray *)progressNormalImages{
    if (!_progressNormalImages) {
        _progressNormalImages = @[[UIImage imageNamed:@"appearance_normal"],[UIImage imageNamed:@"getCar_black"],[UIImage imageNamed:@"pro_black"],[UIImage imageNamed:@"getCar_normal"]];
    }
    return _progressNormalImages;
}
- (NSArray *)progressSelectedImages{
    if (!_progressSelectedImages) {
        _progressSelectedImages = @[[UIImage imageNamed:GetCar_Appoint],[UIImage imageNamed:GetCar_CarInfo],[UIImage imageNamed:GetCar_Proxy],[UIImage imageNamed:Appointment_GetCarSelected]];
    }
    return _progressSelectedImages;
}

- (NSMutableArray *)progressLineLabels{
    if (!_progressLineLabels) {
        _progressLineLabels = [NSMutableArray array];
        for (NSInteger i=0; i<3; i++) {
            UILabel *progressLineLabel = [[UILabel alloc]initWithFrame:CGRectMake(ActualWidth(186)+(ScreenWidth-(2*ActualWidth(186)))*i/3, ActualHeight(21), (ScreenWidth-(2*ActualWidth(186)))/3-ActualWidth(30), 2)];
            progressLineLabel.backgroundColor = ((self.stateDic[[@(i+1) stringValue]])&&(self.stateDic[[@(i) stringValue]]))?TitleColor:[UIColor grayColor];
            [_progressLineLabels addObject:progressLineLabel];
        }
    }
    return _progressLineLabels;
}

- (NSMutableArray *)progressLabels{
    if (!_progressLabels) {
        _progressLabels = [NSMutableArray array];
        for (NSInteger i=0; i<4; i++) {
            UILabel *progressLabel = [[UILabel alloc]initWithFrame:CGRectMake(ActualWidth(171)+(i-0.5)*(ScreenWidth-(2*ActualWidth(186)))/3, ActualHeight(37), (ScreenWidth-(2*ActualWidth(186)))/3, ActualHeight(13))];
            [progressLabel setUpLabelWithText:self.stateDic[[@(i) stringValue]]?self.stateDic[[@(i) stringValue]]:@"" alignment:NSTextAlignmentCenter textSize:12 textColor:[UIColor grayColor]];
            [_progressLabels addObject:progressLabel];
        }
    }
    return _progressLabels;
}


+ (instancetype)createProgressHeaderViewWithStateDic:(NSDictionary *)stateDic andFrame:(CGRect)frame{
    return [[ICMProgressHeaderView alloc]initProgressHeaderViewWithStateDic:stateDic andFrame:frame];
}

@end
